//
//  HNSH_PurseTopView.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/6/26.
//

#import "HNSH_PurseTopView.h"

#import "HNSH_WithdrawalViewController.h"
#import "HNSH_RechargeViewController.h"

@interface HNSH_PurseTopView ()

/* bgView */
@property (nonatomic, strong) UIView *bgView;
/* titleLabel */
@property (nonatomic, strong) UILabel *titleLabel;

/* showIcon */
@property (nonatomic, strong) UIImageView *showIcon;
/* leftButton */
@property (nonatomic, strong) UIButton *leftButton;
/* rightButton */
@property (nonatomic, strong) UIButton *rightButton;

/* bottomBgView */
@property (nonatomic, strong) UIView *bottomBgView;
/* bottomTitleLabel */
@property (nonatomic, strong) UILabel *bottomTitleLabel;
/* timeBtn */
@property (nonatomic, strong) UIButton *timeBtn;

@end

@implementation HNSH_PurseTopView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self configUI];
    }
    return self;
}

- (void)setTime:(NSString *)time{
    _time = time;
    [self.timeBtn setTitle:[NSString stringWithFormat:@"%@", time] forState:UIControlStateNormal];
    [self.timeBtn HNSH_layoutButtonWithEdgeInsetsStyle:HHZXButtonStyleTextLeft imageTitleSpace:4];
}

- (void)configUI {
    [self addSubview:self.bgView];
    [self.bgView addSubview:self.titleLabel];
    [self.bgView addSubview:self.moneyLabel];
    [self.bgView addSubview:self.showIcon];
    [self.bgView addSubview:self.leftButton];
    [self.bgView addSubview:self.rightButton];
    
    [self addSubview:self.bottomBgView];
    [self.bottomBgView addSubview:self.bottomTitleLabel];
    [self.bottomBgView addSubview:self.timeBtn ];
    
    [self.moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.bgView.mas_centerX);
        make.top.mas_equalTo(48);
        make.height.mas_equalTo(45);
    }];
    [self.showIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.moneyLabel.mas_right);
        make.centerY.equalTo(self.moneyLabel.mas_centerY);
        make.width.height.mas_equalTo(20);
    }];
}

//MARK: Getter Methods
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc]initWithFrame:CGRectMake(16, 16+kNavBarAndStatusBarHeight, kScreenWidth-32, 203)];
        _bgView.backgroundColor = [UIColor appWhiteLabelColor];
    }
    return _bgView;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 24, self.bgView.width, 20)];
        _titleLabel.font = [UIFont systemFontOfSize:14];
        _titleLabel.textColor = [UIColor appBlackLabelColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.text = @"总金额";
    }
    return _titleLabel;
}

- (UILabel *)moneyLabel{
    if (!_moneyLabel) {
        _moneyLabel = [[UILabel alloc] init];
        _moneyLabel.font = [UIFont boldSystemFontOfSize:32];
        _moneyLabel.textColor = [UIColor appBlackLabelColor];
        _moneyLabel.text = @"0.00";
    }
    return _moneyLabel;
}

- (UIImageView *)showIcon {
    if (!_showIcon) {
        _showIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"purse_icon_more"]];
        _showIcon.hidden = YES;
    }
    return _showIcon;
}

- (UIButton *)leftButton{
    if (!_leftButton) {
        _leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _leftButton.frame = CGRectMake(16, 137, (self.bgView.width-48)/2, 44);
        [_leftButton setTitle:[NSString stringWithFormat:@"充值"] forState:UIControlStateNormal];
        [_leftButton setTitleColor:[UIColor appWhiteLabelColor] forState:UIControlStateNormal];
        _leftButton.titleLabel.font = [UIFont systemFontOfSize:16];
        _leftButton.layer.cornerRadius = 22;
        _leftButton.layer.masksToBounds = YES;
        _leftButton.backgroundColor = [UIColor appGreenColor];
        [[_leftButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            HNSH_RechargeViewController *vc = [[HNSH_RechargeViewController alloc]init];
            [[UIViewControllerHelper findCurrentShowingViewController].navigationController pushViewController:vc animated:YES];
        }];
    }
    return _leftButton;
}

- (UIButton *)rightButton{
    if (!_rightButton) {
        _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightButton.frame = CGRectMake(32+(self.bgView.width-48)/2, 137, (self.bgView.width-48)/2, 44);
        [_rightButton setTitle:[NSString stringWithFormat:@"提现"] forState:UIControlStateNormal];
        [_rightButton setTitleColor:[UIColor appGreenColor] forState:UIControlStateNormal];
        _rightButton.titleLabel.font = [UIFont systemFontOfSize:16];
        _rightButton.layer.cornerRadius = 22;
        _rightButton.layer.masksToBounds = YES;
        _rightButton.backgroundColor = [UIColor colorWithHexString:@"#FFE9E6"];
        [[_rightButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            HNSH_WithdrawalViewController *vc = [[HNSH_WithdrawalViewController alloc]init];
            [[UIViewControllerHelper findCurrentShowingViewController].navigationController pushViewController:vc animated:YES];
        }];
    }
    return _rightButton;
}

- (UIView *)bottomBgView {
    if (!_bottomBgView) {
        _bottomBgView = [[UIView alloc]initWithFrame:CGRectMake(16, 230+kNavBarAndStatusBarHeight, kScreenWidth-32, 45)];
        _bottomBgView.backgroundColor = [UIColor appWhiteLabelColor];
        [self clipCornerWithView:_bottomBgView cornerWidth:8 cornerType:UIRectCornerTopLeft|UIRectCornerTopRight];
    }
    return _bottomBgView;
}

- (UILabel *)bottomTitleLabel{
    if (!_bottomTitleLabel) {
        _bottomTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 16, 200, 22)];
        _bottomTitleLabel.font = [UIFont boldSystemFontOfSize:16];
        _bottomTitleLabel.textColor = [UIColor appBlackLabelColor];
        _bottomTitleLabel.text = @"资金明细";
    }
    return _bottomTitleLabel;
}

- (UIButton *)timeBtn{
    if (!_timeBtn) {
        _timeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _timeBtn.frame = CGRectMake(kScreenWidth-32-16-87, 16, 87, 24);
        [_timeBtn setImage:[UIImage imageNamed:@"purse_xiala"] forState:UIControlStateNormal];
        [_timeBtn setTitle:[NSString stringWithFormat:@"%@", [NSString getTodayWithFormatter:@"yyyy-MM"]] forState:UIControlStateNormal];
        [_timeBtn setTitleColor:[UIColor appBlackLabelColor] forState:UIControlStateNormal];
        _timeBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        _timeBtn.layer.cornerRadius = 12;
        _timeBtn.layer.masksToBounds = YES;
        _timeBtn.backgroundColor = [UIColor appBGColor];
        [_timeBtn HNSH_layoutButtonWithEdgeInsetsStyle:HHZXButtonStyleTextLeft imageTitleSpace:4];
        WeakSelf(weakSelf);
        [[_timeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            if (weakSelf.chooseTimeBlock) {
                weakSelf.chooseTimeBlock();
            }
        }];
    }
    return _timeBtn;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
